package liangtWorkSpace.simple;

import java.util.ArrayList;

/**
 * @author liangt
 * @ClassName: IsPalindromeNode
 * @title: IsPalindromeNode
 * @projectName liangtWorkSpace.simple
 * @description: 是否是回文链表
 * @date 2020/10/23 12:47
 */
public class IsPalindromeNode {

    static class Node{
        int val;
        Node next;
        Node(int val){
            this.val = val;
        }
    }

    /**
     * 方法一创建数组，对比数组
     * @param head
     * @return
     */
    public static boolean isPalindrome(Node head){
        ArrayList<Integer> arrayList = new ArrayList<>();
        while (head!= null){
            arrayList.add(head.val);
            head = head.next;
        }
        int first = 0;
        int lase = arrayList.size()-1;
        while ( first <lase){
//            if (arrayList.get(first) < 0 ){
//                arrayList.set(first,arrayList.get(first)*-1);
//            }
//            if (arrayList.get(lase) < 0 ){
//                arrayList.set(lase,arrayList.get(lase)*-1);
//            }
            if (!arrayList.get(first).equals(arrayList.get(lase))){
                return false;
            }
            first++;
            lase--;
        }
        return true;
    }

    /**
     * 方法二递归找到最后一个，然后对比
     */
    private static ListNode frontPointer;

    private static boolean reCode(ListNode currentNode){
        if (currentNode!= null){
            if (!reCode(currentNode.next)){
                return false;
            }
            if (currentNode.val != frontPointer.val ){
                return false;
            }
            frontPointer = frontPointer.next;
        }
        return true;
    }
    public static boolean isPalindrome2(ListNode head){
        frontPointer = head;
        return reCode(head);
    }
    /**
     * 翻转链表
     */
    public static Node reCode(Node oleNode,Node newNode){
        System.out.println("oldNode = "+oleNode.val);
        System.out.println("******************");
        Node temp = newNode;
        if (oleNode.next == null){
            newNode = oleNode;
        }else {
            newNode = reCode(oleNode.next,newNode);
            System.out.println(temp.val);
            newNode.next = temp;
        }
        System.out.println("newNode = "+newNode.val);
        return newNode;
    }


    /**
     * 方法三；找到中间点，反转
     * @param head
     * @return
     */
    public boolean isPalindrome(ListNode head) {
        if (head == null) {
            return true;
        }

        // 找到前半部分链表的尾节点并反转后半部分链表
        ListNode firstHalfEnd = endOfFirstHalf(head);
        ListNode secondHalfStart = reverseList(firstHalfEnd.next);

        // 判断是否回文
        ListNode p1 = head;
        ListNode p2 = secondHalfStart;
        boolean result = true;
        while (result && p2 != null) {
            if (p1.val != p2.val) {
                result = false;
            }
            p1 = p1.next;
            p2 = p2.next;
        }

        // 还原链表并返回结果
        firstHalfEnd.next = reverseList(secondHalfStart);
        return result;
    }

    private ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode nextTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return prev;
    }

    private ListNode endOfFirstHalf(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public static void main(String[] args) {

        Node a = new Node(1);
        Node b = new Node(2);
        Node c = new Node(3);
        Node d = new Node(3);
        Node e = new Node(2);
        Node f = new Node(1);
        a.next = b;
        b.next = c;
        c.next = d;
        d.next = e;
        e.next = f;
        Node first = new Node(-129);
        Node last = new Node(-129);
        first.next=last;
        System.out.println(isPalindrome(first));

        ListNode firstNode = new ListNode(-129);
        ListNode lastNode = new ListNode(-129);
        firstNode.next=lastNode;
        System.out.println(isPalindrome2(firstNode));

        System.out.println(new IsPalindromeNode().isPalindrome(firstNode));
//        Node newNode = new Node(0);
//        newNode = reCode(a,newNode);
//        System.out.println( "b.Node="+ newNode.val);
    }
}
